#include "Pch.h"

#include "../../MCD/Core/System/WindowEvent.h"
#include "../../MCD/Core/System/Log.h"
#include "../RenderAdv/Dx11/Dx11Window.h"
#include "../RenderAdv/Dx11/Dx11FixedFunctionPipeline.h"

using namespace MCD;

TEST(RenderWindowTest)
{
	Dx11Window window1;
	window1.create(L"title='Window1';width=400;height=300");
	window1.setOptions(L"showCursor=1");

	Dx11Window window2;
	window2.create(L"title='Window2';width=400;height=300");
	window2.setOptions(L"showCursor=0");

	{	// make sure ffp is destroyed before the windows
		Dx11FixedFunctionPipeline ffp;
		Dx11FixedFunctionPipeline::Vertex v[3];
		v[0].position = Vec3f(0,0.5f,0);
		v[0].color = Vec4f(1,0,0,1);

		v[1].position = Vec3f(0.5f,-0.5f,0);
		v[1].color = Vec4f(0,1,0,1);

		v[2].position = Vec3f(-0.5f,-0.5f,0);
		v[2].color = Vec4f(0,0,1,1);

		Log::format(Log::Info, L"RenderWindowTest start rendering...");

		while(true)
		{
			Event e;
			// check for window1 events
			if(window1.popEvent(e, false))
			{
				if(e.Type == Event::Closed)
					break;

				if(e.Type == Event::KeyPressed && e.Key.Code == Key::F1)
					window1.setVerticalSync(true);

				if(e.Type == Event::KeyPressed && e.Key.Code == Key::F2)
					window1.setVerticalSync(false);

				if(e.Type == Event::KeyPressed && e.Key.Code == Key::Num1)
					window1.setOptions(L"MSAA=1");

				if(e.Type == Event::KeyPressed && e.Key.Code == Key::Num2)
					window1.setOptions(L"MSAA=8");
			}
			
			// check for window2 events
			if(window2.popEvent(e, false))
			{
				if(e.Type == Event::Closed)
					break;
			}

			// Draw to window 1
			window1.makeActive();
			window1.clearColorBuffer(Vec4f(0.2f, 0.2f, 0.2f, 1.0f).data);
			window1.clearDepthStencilBuffer(1.0f, 0);
			
			ffp.begin();

			ffp.drawTriangleList(v, 3);

			ffp.end();

			window1.swapBuffers();

			// Draw to window 2
			window2.makeActive();
			window2.clearColorBuffer(Vec4f(0.2f, 0.2f, 0.5f, 1.0f).data);
			window2.swapBuffers();
		}
	}

	window1.destroy();
	window2.destroy();
}